from django.http import JsonResponse
from django.shortcuts import render
from myking.models import AreaInfo


def areas(request):
    area=AreaInfo.objects.get(title='贵阳')
    parent=area.fatherArea
    children=area.areainfo_set.all()
    data={
        'area':area,
        'parent':parent,
        'children':children
    }
    return render(request,'areas.html',data)
# Create your views here.

def prov(request):
    areas =AreaInfo.objects.filter(fatherArea_id=0)
    provs=[]
    for p in areas:
        provs.append((p.id,p.title))
    return JsonResponse({'data':provs})

def city(request,pid):
    citys=AreaInfo.objects.filter(fatherArea_id=pid)
    cityList=[]
    for c in citys:
        cityList.append((c.id,c.title))
    return JsonResponse({'data':cityList})